package net.spatula.tally_ho.ui.behaviors;

/* Copyright 2006 Nicklas R Johnson
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

import java.lang.reflect.Method;
import java.util.List;

import net.spatula.tally_ho.utils.TextUtils;

import wicket.Component;
import wicket.behavior.AbstractBehavior;
import wicket.behavior.HeaderContributor;
import wicket.behavior.StringHeaderContributor;
import wicket.markup.ComponentTag;

public class DropDownLabelUpdateBehavior extends AbstractBehavior {

	private static final long serialVersionUID = 1L;

	private static final String javaScriptStart = "<script type=\"text/javascript\">\n";

	private static final String javaScriptEnd = "</script>\n";

	private String labelName;
	
	private String value;

	private String id;

	private List beans;

	public DropDownLabelUpdateBehavior(List beans, String labelName,
			String idName, String valueName) {
		this.beans = beans;
		this.id = idName;
		this.value = valueName;
		this.labelName = labelName;

	}

	public void bind(Component component) {
		component.add(HeaderContributor.forJavaScript(this.getClass(),
				"dropDownLabelUpdateBehavior.js"));
		component.setOutputMarkupId(true);
		component.add(new StringHeaderContributor(javaScriptStart
				+ getMappings() + javaScriptEnd));
	}

	public void onComponentTag(Component component, ComponentTag tag) {
		tag.getAttributes().remove("onchange");
		tag.getAttributes().add(
				"onchange",
				"updateLabel('" + component.getMarkupId() + "', '" + labelName
						+ "')");

	}

	private String getMappings() {

		if (id == null || id.length() < 1 || value == null
				|| value.length() < 1) {
			return "";
		}
		String idMethod = TextUtils.makeGetterName(id);
		String valueMethod = TextUtils.makeGetterName(value);

		StringBuffer map = new StringBuffer(100);

		for (Object object : beans) {
			Method getId;
			Method getValue;
			String thisid, thisvalue;
			try {
				getId = object.getClass().getMethod(idMethod, (Class[]) null);
				getValue = object.getClass().getMethod(valueMethod,
						(Class[]) null);
				thisid = (getId.invoke(object, (Object[]) null)).toString();
				thisvalue = (getValue.invoke(object, (Object[]) null))
						.toString();
			} catch (Exception e) {
				// Just make a best effort, and skip the element if anything
				// goes wrong.
				continue;
			}
			map.append("updateMap[\"" + labelName + "." + thisid + "\"] = \""
					+ thisvalue + "\";\n");
		}

		return map.toString();
	}
}
